<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class UserController extends BaseController {

    //考生列表
	public function index() {
        //筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['user_nickname'])) {
            $data['user_nickname'] = array('like',"%".trim($_GET['user_nickname'])."%");
        }
        if (!empty($_GET['user_phone'])) {
            $data['user_phone'] = array('like',"%".trim($_GET['user_phone'])."%");
        }
        unset($data['p']);
        $data['user_display'] = 1;
        $data['user_role'] = 1;
        $count = M('user')->where($data)->count();
        $page = getpage($count,10);
        $user_arr = M('user')->field('user_id,user_nickname,user_phone,user_auth,user_reg_time')->where($data)->order('user_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('user_arr', $user_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //删除考生
    public function user_del(){
        $data['user_id'] = I('get.user_id');
        $data['user_display'] = 0;
        if(M('user')->save($data)){
            $this->redirect('index','删除考生成功');
        }else{
            $this->error('删除考生失败');
        }
    }

    //考生详情
    public function user_details(){
        $data['user_id'] = I('get.user_id');
        $user_arr = M('user')->alias('u')->join('left join midi_city as c on u.user_city = c.city_id')->where($data)->find();
        unset($user_arr['user_password']);
        $this->assign('user_arr',$user_arr);
        $this->display();
    }

    //修改考生信息
    public function user_edit(){
        if (I('post.action') == 'user_edit_action') {
            $this->user_edit_action();
            exit();
        }
        $data['user_id'] = I('get.user_id');
        $user_arr = M('user')->where($data)->find();
        $city_arr = M('city')->select();
        $this->assign('city_arr', $city_arr);
        $this->assign('user_arr',$user_arr);
        $this->display();
    }

    //修改考生信息操作
    public function user_edit_action(){
        $finfo = upload($_FILES['avatar']);
        if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['user_avatar'] = $finfo['filepath'];
        }
        if(M('user')->save($_POST)){
            $this->success('修改信息成功');
        }else{
            $this->error('没有信息被修改');
        }
    }

}